package com.csweimei.rentmanagementtreasure.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * <文件公共类>
 *
 * @author caoyinfei
 * @version [版本号, 2016/6/6]
 * @see [相关类/方法]
 * @since [V1]
 */
public class FileUtils {






    /**
     * 针对系统文夹只需要扫描,不用插入内容提供者,不然会重复
     *
     * @param context  上下文
     * @param filePath 文件路径
     */
    public static void scanFile(Context context, String filePath) {
        if (isNotExists(filePath))
            return;
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(new File(filePath)));
        context.sendBroadcast(intent);
    }

    public static boolean deleteFiles(String folderPath) {
        List<File> files = getFileFromFolder(folderPath);
        if (files.size() != 0) {
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);

                /**  如果是文件则删除  如果都删除可不必判断  */
                if (file.isFile()) {
                    file.delete();
                }

            }
        }
        return true;
    }




    public static boolean deleteFiles(ContentResolver cr , String folderPath) {
        List<File> files = getFileFromFolder(folderPath);
        if (files.size() != 0) {
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);

                /**  如果是文件则删除  如果都删除可不必判断  */
                if (file.isFile()) {
//                    file.delete();
//                    ImageUtils.delImage(cr ,file.getAbsolutePath());
                    file.delete();
                }

            }
        }
        return true;
    }


    public static List<File> getFileFromFolder(String folderPath) {
        File folder = new File(folderPath);
        List<File> list = new ArrayList<>();
        File[] fileArray = folder.listFiles();
        if (fileArray == null) {
            return list;
        } else {
            for (File f : fileArray) {
                if (f.isFile()) {
                    list.add(0, f);
                } else {
                    getFileFromFolder(f.getAbsolutePath());
                }
            }
        }
        return list;
    }

    public static ContentValues getVideoContentValues(Context paramContext, File paramFile, long paramLong) {
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("title", paramFile.getName());
        localContentValues.put("_display_name", paramFile.getName());
        localContentValues.put("mime_type", "video/3gp");
        localContentValues.put("datetaken", Long.valueOf(paramLong));
        localContentValues.put("date_modified", Long.valueOf(paramLong));
        localContentValues.put("date_added", Long.valueOf(paramLong));
        localContentValues.put("_data", paramFile.getAbsolutePath());
        localContentValues.put("_size", Long.valueOf(paramFile.length()));
        return localContentValues;
    }

    /**
     * 创建文件
     *
     * @param filePath 文件地址
     * @param fileName 文件名
     * @return
     */
    public static boolean createFile(String filePath, String fileName) {

        String strFilePath = filePath + File.separator +fileName;

        File file = new File(filePath);
        if (!file.exists()) {
            /**  注意这里是 mkdirs()方法  可以创建多个文件夹 */
            file.mkdirs();
        }

        File subfile = new File(strFilePath);

        if (!subfile.exists()) {
            try {
                boolean b = subfile.createNewFile();
                return b;
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            return true;
        }
        return false;
    }

    /**
     * 文件是否已存在
     *
     * @param file
     * @return
     */
    public static boolean isFileExist(File file) {
        if (file.exists()) {
            return true;
        }
        return false;
    }

    /**
     * @param filePath 删除的文件路径
     * @return 是否删除成功
     */

    public static boolean deleteFile(String filePath) {

        File file = new File(filePath);
        if (file.exists() && file.isFile())
            file.delete();
        return true;
    }

    /**
     * 文件不存在，返回true
     *
     * @param file
     * @return
     */
    public static boolean isNotFileExist(File file) {
        if (file.exists()) {
            return false;
        }
        return true;
    }

    /**
     * 删除指定文件
     *
     * @param path
     */
    public static void isDel(String path) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    // 获取sdcard的目录
    public static String getSDPath(Context context) {
        // 判断sdcard是否存在
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // 获取根目录
            return Environment.getExternalStorageDirectory().getPath();
        }
        return context.getFilesDir().getPath();
    }

    /**
     * 写入字幕文件
     */
//    public static void createSubtitleFile(ArrayList<SubtitleModel> subtitleList, String subtitlePath) {
//        StringBuffer text = new StringBuffer();
//        text.append("WEBVTT\n\n");
//        for (int i = 0; i < subtitleList.size() - 1; i++) {
//            String start = GeneralUtils.formatTime((long) subtitleList.get(i).getStart());
//            String end = GeneralUtils.formatTime((long) subtitleList.get(i).getEnd());
//            //   text.append((i + 1) + "\n");
//            text.append(start + " --> " + end + "\n");
//            text.append(subtitleList.get(i).getContext() + "\n\n");
//            //  Log.i(TAG, "createSubtitleFile: " + text.toString());
//        }
//        try {
//            File file = new File(subtitlePath);
//            if (FileUtil.isFileExist(file)) {
//                file.delete();
//            }
//            file.getParentFile().mkdirs();
//            file.createNewFile();
//            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//            raf.seek(file.length());
//            raf.write(text.toString().getBytes());
//            raf.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 保存音频文件
     *
     * @param is InputStream
     */
    public static void saveFile(InputStream is, String path) throws IOException {
        // 构建文件输出流
        FileOutputStream fos = new FileOutputStream(new File(path));
        byte[] b = new byte[1024]; // 构建缓冲区
        // 循环读取数据
        int byteCount; // 每次读取的长度
        while ((byteCount = is.read(b)) != -1) {
            fos.write(b, 0, byteCount); // 将每次读取的数据保存到文件当中
        }
        fos.close(); // 关闭文件输出流
        is.close(); // 关闭输入流
    }
    //
    //    /**
    //     * 构造文件处理工具类 - 播放音频文件
    //     */
    //    public FileUtil() {
    //        // 音频播放的缓存文件 构造文件根目录
    //        file = new File(Constants.BG_MUSIC_PATH);
    //        if (!file.exists()) { // 判断文件夹是否存在
    //            // 不存在，则创建文件夹
    //            file.mkdirs();
    //        }
    //    }

    /**
     * 判断本地是否有指定url音频文件的缓存文件
     *
     * @param url 音频文件url
     * @return 缓存文件路径, 如果不存在则返回null
     */
//    public static String exists(String url) throws IOException {
//        // 截取地址, 获取缓存音频文件的名称
//        String fileName = url.substring(url.lastIndexOf("/") + 1);
////        LogUtils.i("FileUtil", fileName);
//        // 构建文件
////        File tempFile = new File(Constants.TEMPORARY_MUSIC_PATH, fileName);
//        // 如果文件存在,则返回文件路径,如果文件不存在则返回null并且创建文件
//        if (tempFile.exists()) {
//            return tempFile.getAbsolutePath();
//        } else {
//            tempFile.createNewFile(); // 创建音频文件
//            return null;
//        }
//    }

    /**
     * 删除某个文件
     */
    public static void delFile(String filePath) {
        File f = new File(filePath);
        if (f.exists()) {
            f.delete();
        }
    }

    /**
     * 新建目录
     *
     * @param path
     * @return
     */
    public static void createNewDir(String path) {
        File dir = new File(path);
        if (!FileUtils.isFileExist(dir)) {
            boolean isSuc = dir.mkdirs();
//            LogUtils.i("FileUtil", isSuc + "isSuc");
        }
//        LogUtils.i("FileUtil", dir.exists() + " isExists");
    }


    /**
     * 新建文件
     *
     * @param path
     * @return
     */
    public static String createNewFile(String path) {
        try {
            File file = new File(path);
            if (file == null || !file.exists()) {
                //            file.mkdir();
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    /**
     * 写入文本 list.txt
     * 音视频拼接时使用
     */
    public static void writeFileToSd(int num, String path, String destTxtPath) {
        String filePath = new File(path).getAbsolutePath();
        StringBuffer concatText = new StringBuffer();
        for (int i = 1; i < num; i++) {
            concatText.append("file '" + filePath + "'\n");
        }
        concatText.append("file '" + filePath + "'");
        try {
            File file = new File(destTxtPath);
            if (FileUtils.isFileExist(file)) {
                file.delete();
            }
            file.getParentFile().mkdirs();
            file.createNewFile();
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            raf.seek(file.length());
            raf.write(concatText.toString().getBytes());
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入文本 imgList.txt
     */
//    public static void writeFileToSd(List<String> imgPaths, int duration) {
//        //        String filePath = new File(Constants.IMG_FILE_PATH).getAbsolutePath();
//        //        String text = "file '" + filePath + "'\n " + "file '" + filePath + "'";
//        StringBuffer text = new StringBuffer();
//        for (int i = 0; i < imgPaths.size(); i++) {
//            //            text.append("file '" + imgsPath.get(i) + "'\n" + "duration 3\n");
//            text.append("file '" + imgPaths.get(i) + "'\n" + "duration " + duration + "\n");
//        }
//        String lastFile = text.substring(text.lastIndexOf("file"), text.length());
//        text.append(lastFile);
//        text.delete(text.lastIndexOf("duration " + duration), text.length());
//        //        text.delete(text.lastIndexOf("duration 3"), text.length());
//        Log.i("FileUtil", text.toString());
//        try {
//            File file = new File(Constants.splicingFile);
//            if (file.exists()) {
//                file.delete();
//            }
//            file.getParentFile().mkdirs();
//            file.createNewFile();
//            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//            raf.seek(file.length());
//            raf.write(text.toString().getBytes());
//            raf.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 获取文件大小
     *
     * @param context
     * @param filePath
     * @return
     */
    public static long getFileSize(Context context, String filePath) {
        Uri uri = Uri.parse(filePath);
        // String[] proj = {MediaStore.Audio.Media.SIZE};
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Audio.Media.
                    SIZE}, null, null, null);
//            LogUtils.i("FileUtil", "getFileSize: " + (cursor == null) + "," + uri.toString());
            if (null != cursor && cursor.moveToFirst()) {
                long aLong = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.
                        SIZE));
                return aLong;
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return 0;
    }

    /**
     * 获取sd卡所有的音频文件
     *
     * @param context
     * @param page 从1开始
     * @param context
     * @return
     * @throws Exception
     */
    private static final String[] SUPPORT_DECODE_AUDIOFORMAT = {"audio/mpeg", "audio/x-ms-wma",
            "audio/mp4a-latm"};

//    public static ArrayList<AudioEntry> getLocalAudios(Context context) {
//        ArrayList<AudioEntry> audios = null;
//        StringBuilder selectionBuilder = new StringBuilder();
//        int size = SUPPORT_DECODE_AUDIOFORMAT.length;
//        for (int i = 0; i != size; ++i) {
//            selectionBuilder.append("mime_type=? or ");
//        }
//        int sbLen = selectionBuilder.length();
//        selectionBuilder.delete(sbLen - 3, sbLen);
//
//        final String selection = selectionBuilder.toString();
//        //        final String orderBy = String.format("%s LIMIT %s , %s ", MediaStore.Audio.Media.DEFAULT_
//        // SORT_ORDER, (page-1)*pageSize , pageSize);
//        Cursor cursor = context.getContentResolver().query(
//                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
//                new String[]{MediaStore.Audio.Media._ID,
//                        MediaStore.Audio.Media.DISPLAY_NAME,
//                        MediaStore.Audio.Media.TITLE,
//                        MediaStore.Audio.Media.DURATION,
//                        MediaStore.Audio.Media.ARTIST,
//                        MediaStore.Audio.Media.ALBUM,
//                        MediaStore.Audio.Media.YEAR,
//                        MediaStore.Audio.Media.MIME_TYPE,
//                        MediaStore.Audio.Media.SIZE,
//                        MediaStore.Audio.Media.DATA},
//                selection,
//                SUPPORT_DECODE_AUDIOFORMAT, null);
//
//        audios = new ArrayList<>();
//
//        if (cursor.moveToFirst()) {
//            AudioEntry audioEntry = null;
//            String fileUrl = null;
//            do {
//                fileUrl = cursor.getString(9);
//                //                isMatchAudioFormat = MediaUtils.isMatchAudioFormat(fileUrl, 44100, 2);
//                audioEntry = new AudioEntry();
//                audioEntry.id = cursor.getLong(0);
//                // 文件名
//                audioEntry.fileName = cursor.getString(1);
//                // 歌曲名
//                audioEntry.title = cursor.getString(2);
//                // 时长
//                audioEntry.duration = cursor.getInt(3);
//                // 歌手名
//                audioEntry.artist = cursor.getString(4);
//                // 专辑名
//                audioEntry.album = cursor.getString(5);
//                // 年代
//                audioEntry.year = cursor.getString(6);
//                // 歌曲格式
//                audioEntry.mime = cursor.getString(7).trim();
//                // 文件大小
//                audioEntry.size = cursor.getString(8);
//                // 文件路径
//                audioEntry.fileUrl = fileUrl;
//                audios.add(audioEntry);
//            } while (cursor.moveToNext());
//            cursor.close();
//        }
//        return audios;
//    }

    /**
     * 得到目录下的所有文件
     *
     * @param strPath
     * @return
     */
    public static ArrayList<String> getFilePath(String strPath) {
        ArrayList<String> fileList = new ArrayList<>();
        File dir = new File(strPath);
        File[] files = dir.listFiles();
        if (files == null) {
            return fileList;
        }
        for (int i = 0; i < files.length; i++) {
            String filePath = files[i].getAbsolutePath();
            fileList.add(filePath);
        }
        return fileList;
    }


    /**
     * 判断指定目录是否有文件存在
     *
     * @param path
     * @param fileName
     * @return
     */
    public static File getFiles(String path, String fileName) {
        File f = new File(path);
        File[] files = f.listFiles();
        if (files == null) {
            return null;
        }

        if (null != fileName && !"".equals(fileName)) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (fileName.equals(file.getName())) {
                    return file;
                }
            }
        }
        return null;
    }

    /**
     * 判断指定目录是否有文件存在
     *
     * @param path
     * @param imgName
     * @return
     */
    public static String getImgPathName(String path, String imgName) {
        File f = new File(path);
        File[] files = f.listFiles();
        if (files == null) {
            return null;
        }
        if (null != imgName && !"".equals(imgName)) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                String startName = file.getName().substring(0, file.getName().lastIndexOf("."));
                Log.i("FileUtil", "getImg: " + startName);
                if (imgName.equals(startName)) {
                    return file.getAbsolutePath();
                }
            }
        }
        return null;
    }

    /**
     * 删除目录（文件夹）下的文件
     *
     * @return 目录删除成功返回true，否则返回false
     */
    public static void deleteDir(String path) {
        File dirFile = new File(path);
        File[] files = dirFile.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                // 删除子文件
                if (files[i].isFile()) {
                    files[i].delete();
                }
                // 删除子目录
                else {
                    deleteDir(files[i].getAbsolutePath());
                }
            }
        }
    }

    /**
     * 获取目录下的所有MP3文件
     */
    public static List<String> getVoiceFilesName(String path, String endsWithName) {
        List<String> voices = new ArrayList<>();
        //        File file = new File(Constants.WORK_PATH);
        File file = new File(path);
        File[] subFile = file.listFiles();
//        LogUtils.i("FileUtils", (subFile == null) + "");
        if (subFile == null) {
            return null;
        }
        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
            // 判断是否为文件夹
            if (!subFile[iFileLength].isDirectory()) {
                String filename = subFile[iFileLength].getName();
                // 判断是否为mp3、wav结尾
                if (filename.trim().toLowerCase().endsWith(endsWithName) || filename.trim().
                        toLowerCase().endsWith(".wav")) {
                    voices.add(filename);
                }
            }
        }
        return voices;
    }

    /**
     * 根据文件路径获取文件名
     *
     * @return
     */
    public static String getFileName(String path) {
        if (path != null && !"".equals(path.trim())) {
            return path.substring(path.lastIndexOf("/"));
        }

        return "";
    }

    public static boolean isNotExists(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return true;
        }
        return false;
    }

    public static boolean isExists(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            return true;
        }
        return false;
    }

    /**
     * 保存图片
     */
    public static void moveFile(String srcFilePath, String destFilePath) {
        File srcFile = new File(srcFilePath);
        if (!srcFile.exists() || !srcFile.isFile()) {
            return;
        }
        File destDir = new File(destFilePath);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        srcFile.renameTo(new File(destFilePath));
    }

    /**
     * 删除目录下的所有文件
     *
     * @param path
     * @return
     */
    public static boolean delFiles(String path) {
        File f = new File(path);
        if (!f.exists())
            return false;
        if (f.isFile()) {
            f.delete();
            return true;
        }
        File dir = new File(path);
        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            return false;
        }
        for (File file : files) {
            file.delete();
        }
        return true;
    }

    /**
     * @param filePath
     * @return
     * @function 抽取文件路径中的文件名
     */
    public static String getFileNameFromFilePath(String filePath) {
        int begin = filePath.indexOf("\\");
        String path = filePath.substring(begin + 1);
        while (true) {
            begin = path.indexOf("\\");
            if (begin >= 0)
                path = path.substring(begin + 1);
            else
                break;
        }
        int end = path.indexOf(".");
        return path.substring(0, end);
    }

    /**
     * 写入文本 list.txt
     * 音视频拼接时使用
     */
    public static void writeFileToSd2(List<String> pathList, String destTxtPath) {
        StringBuffer concatText = new StringBuffer();
        for (int i = 0; i < pathList.size() - 1; i++) {
            concatText.append("file '" + pathList.get(i) + "'\n");
            Log.i("FileUtils", "musicJoint: " + pathList.get(i));
        }
        concatText.append("file '" + pathList.get(pathList.size() - 1) + "'");
        try {
            File file = new File(destTxtPath);
            if (FileUtils.fileIsExists(destTxtPath)) {
                file.delete();
            }
            file.getParentFile().mkdirs();
            file.createNewFile();
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            raf.seek(file.length());
            raf.write(concatText.toString().getBytes());
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     * @return boolean
     */
    public static boolean copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
                fs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @param context
     */
    public static String assetToSD(Context context, String assetPath, String destPath) {
        File newFile = new File(destPath);
        try {
            InputStream inputStream = context.getAssets().open(assetPath);
            try {
                FileOutputStream outputStream = new FileOutputStream(newFile);
                try {
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0) {
                        outputStream.write(buf, 0, len);
                    }
                } finally {
                    outputStream.close();
                }
            } finally {
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newFile.getAbsolutePath();
    }


    /**
     * 重命名文件
     *
     * @param oldPath 原来的文件地址
     * @param newPath 新的文件地址
     */
    public static boolean renameFile(String oldPath, String newPath) {
        File oleFile = new File(oldPath);
        File newFile = new File(newPath);
        //执行重命名
        return oleFile.renameTo(newFile);
    }

    /**
     * 判断文件是否存在
     */
    public static boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static byte[] File2Bytes(File file) {
        int byte_size = 1024;
        byte[] b = new byte[byte_size];
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(
                    byte_size);
            for (int length; (length = fileInputStream.read(b)) != -1;) {
                outputStream.write(b, 0, length);
            }
            fileInputStream.close();
            outputStream.close();
            return outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 转换文件
    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 此类用于生成合并视频所需要的文档
     * @param strcontent 视频路径集合
     * @param filePath 生成的地址
     * @param fileName 生成的文件名
     */
    public static void writeTxtToFile(List<String> strcontent, String filePath, String fileName) {
        //生成文件夹之后，再生成文件，不然会出错
        makeFilePath(filePath, fileName);
        String strFilePath = filePath + fileName;
        // 每次写入时，都换行写
        String strContent = "";
        for (int i = 0; i < strcontent.size(); i++) {
            strContent += "file " + strcontent.get(i) + "\r\n";
        }
        try {
            File file = new File(strFilePath);
            //检查文件是否存在，存在则删除
            if (file.isFile() && file.exists()) {
                file.delete();
            }
            file.getParentFile().mkdirs();
            file.createNewFile();
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            raf.seek(file.length());
            raf.write(strContent.getBytes());
            raf.close();
            Log.e("TestFile", "写入成功:" + strFilePath);
        } catch (Exception e) {
            Log.e("TestFile", "Error on write File:" + e);
        }
    }

    //创建路径
    public static File makeFilePath(String filePath, String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(filePath + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    //创建文件夹
    public static void makeRootDirectory(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            }
        } catch (Exception e) {
            Log.i("error:", e + "");
        }
    }
}